package com.lankystar.sos.action;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.Servlet;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.lankystar.sos.service.IRoomInterface;
import com.lankystar.util.ResUtil;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("sos")
@Namespace("/")
@Component
@Scope("prototype")
@Results({
	@Result(name="success",type="json")
})
public class RoomAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	@Autowired
	private IRoomInterface roomInterface;
	private String message;
	private Integer sos_id;
	private Integer userId;
	private String pageSize;
	private String pageNow;
	private String type;
	
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Integer getSos_id() {
		return sos_id;
	}
	public void setSos_id(Integer sos_id) {
		this.sos_id = sos_id;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	public String getPageSize() {
		return pageSize;
	}
	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}
	public String getPageNow() {
		return pageNow;
	}
	public void setPageNow(String pageNow) {
		this.pageNow = pageNow;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	@Action(value="sendMessage")
	public String sendMessageMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			//userId 发消息的人、sos_id 聊天的室(求救单id)、message 消息内容
			roomInterface.saveMessageMethod(userId, sos_id, message);
			map.put("code", 1);
			map.put("msg", "消息发送成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "消息发送失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	
	@Action(value="receiveMessage")
	public String receiveMessageMethod(){
		Map<String, Object> map = new HashMap<>();
		try {
			pageNow = pageNow==null?"1":pageNow;
			pageSize = pageSize==null?"10":pageSize;
			
			map.put("data", roomInterface.queryMessageBysos_idMethod(sos_id,Integer.parseInt(pageSize),Integer.parseInt(pageNow)));
			map.put("code", 1);
			map.put("msg", "聊天查询成功！！");
		} catch (Exception e) {
			map.put("code", 0);
			map.put("msg", "聊天查询失败！！");
		}
		ResUtil.toJson(ServletActionContext.getResponse(), map);
		return SUCCESS;
	}
	
	
}
